<template>
	<view class="content">
		<u-navbar back-text="" title="">
			<view style="width: 100%;display: flex;justify-content: center;font-weight: bold;font-size: 34rpx;margin-left: 25rpx;">
				申请记录
			</view>
			<view slot="right">
				<view @click="goaddfriend" style="margin-right: 15rpx;color: #7a7a7a;">
					添加好友
				</view>
			</view>
		</u-navbar>
		<view style="width: 100%;padding: 15rpx;">
			<u-subsection @change="change" :list="typelist" :current="current"></u-subsection>
		</view>
		<view v-if="current == 0" style="display: flex;flex-direction: column;width: 100%;flex: 1;overflow-y: auto;">
			<view @click="handleApply(item)" v-for="(item,k) in list0"
				style="width: 100%;display: flex;align-items: center;justify-content: space-between;padding: 20rpx;border-bottom: 1rpx solid #f4f4f4;">
				<view style="display: flex;align-items: center;">
					<u-avatar :src="$store.state.common.systemInfo.fileurl + 'avatar/' + item.fromid + '.jpg'"></u-avatar>
					<view style="display: flex;flex-direction: column;margin-left: 10rpx;">
						<view style="font-size: 32rpx;font-weight: 500;">
							{{item.fromname}}
						</view>
						<view style="color: #cacaca;">
							{{item.message}}
						</view>
					</view>
				</view>
				<view style="display: flex;flex-direction: column;justify-content: flex-end;">
					<view v-if="item.status == '申请中'" style="color: #ffaa00;display: flex;justify-content: flex-end;">
						申请中
					</view>
					<view v-if="item.status == '已通过'" style="color: #00aa7f;display: flex;justify-content: flex-end;">
						已通过
					</view>
					<view v-if="item.status == '已拒绝'" style="color: #ff557f;display: flex;justify-content: flex-end;">
						已拒绝
					</view>
					<view style="display: flex;justify-content: flex-end;color: #9c9c9c;font-size: 24rpx;">
						{{$u.timeFormat(item.time, 'mm-dd hh:MM:ss')}}
					</view>
				</view>
			</view>
		</view>
		<view v-if="current == 1" style="display: flex;flex-direction: column;width: 100%;flex: 1;overflow-y: auto;">
			<view v-for="(item,k) in list1"
				style="width: 100%;display: flex;align-items: center;justify-content: space-between;padding: 20rpx;border-bottom: 1rpx solid #f4f4f4;">
				<view style="display: flex;align-items: center;">
					<u-avatar v-if="item.gid == 0" :src="$store.state.common.systemInfo.fileurl + 'avatar/' + item.toid + '.jpg'"></u-avatar>
					<view class="" v-else>
						<bgyx-group-avatar :id="item.gid" />
					</view>
					<view style="display: flex;flex-direction: column;margin-left: 10rpx;">
						<view style="font-size: 32rpx;font-weight: 500;">
							{{item.toname}}
						</view>
						<view style="color: #cacaca;">
							{{item.message}}
						</view>
					</view>
				</view>
				<view style="display: flex;flex-direction: column;justify-content: flex-end;">
					<view v-if="item.status == '申请中'" style="color: #7a7a7a;display: flex;justify-content: flex-end;">
						申请中
					</view>
					<view v-if="item.status == '已通过'" style="color: #00aa7f;display: flex;justify-content: flex-end;">
						已通过
					</view>
					<view v-if="item.status == '已拒绝'" style="color: #ff557f;display: flex;justify-content: flex-end;">
						已拒绝
					</view>
					<view style="display: flex;justify-content: flex-end;color: #9c9c9c;font-size: 24rpx;">
						{{$u.timeFormat(item.time, 'mm-dd hh:MM:ss')}}
					</view>
					<!-- <view v-if="item.status == '申请中'" class="bgyxtag" style="background: #aeb5cc;color: white;border-radius: 8rpx;font-size: 20rpx;">
						申请中
					</view> -->
				</view>
			</view>
		</view>
		
		<u-action-sheet @click="applyres" :list="hdlist" v-model="show"></u-action-sheet>
	</view>
</template>

<script>
	import moment from 'moment'
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				typelist: [{
					name: "好友申请"
				}, {
					name: "我的申请"
				}],
				current: 0,
				show:false,
				hdlist:[{text:'通过'},{text:'拒绝'}],
				currentdata:{}
			}
		},
		onLoad() {
			this.$store.dispatch({
				type:"apply/init",
				payload:{}
			})
		},
		computed: {
			...mapState({
				list0: (state) => {
					const user = uni.getStorageSync("user")
					return state.apply.list.filter(e=>{
						return e.toid == user.id
					})
				},
				list1: (state) => {
					const user = uni.getStorageSync("user")
					return state.apply.list.filter(e=>{
						return e.fromid == user.id
					})
				}
			})
		},
		methods: {
			goaddfriend:function(){
				uni.navigateTo({
					url:"/pages/friend/addfriend"
				})
			},
			change: async function(e) {
				this.current = e
			},
			applyres: async function(e) {
				console.log(e)
				if(e == 0){
					const ret = await this.$u.put(`friend/friend/reuserapply/agree`,{
						...this.currentdata,
						status:"已通过",
					})
					
					this.$socket.send({
						type:"touser_passapply",
						data:{
							to:{
								id:this.currentdata.fromid,
								name:this.currentdata.fromname
							},
							item:this.currentdata
						}
					})
					
					this.$store.commit(`apply/setApplyStatus`,{key:this.currentdata.id,data:'已通过'})
					this.$store.dispatch({
						type:"friend/addfriendbyid",
						payload:this.currentdata.fromid
					})
				}
				if(e == 1){
					const ret = await this.$u.put(`friend/friend/reuserapply/refuse`,{
						...this.currentdata,
						status:"已拒绝",
					})
					
					this.$socket.send({
						type:"touser_refuseapply",
						data:{
							to:{
								id:this.currentdata.fromid,
								name:this.currentdata.fromname
							},
							item:this.currentdata
						}
					})
					
					this.$store.commit(`apply/setApplyStatus`,{key:this.currentdata.id,data:'已拒绝'})
				}
				
				this.$store.commit(`tabbar/decApplyCount`,{})
			},
			handleApply: async function(e) {
				if(e.status == '申请中'){
					this.show = true
					this.currentdata = e
				}
			},
		}
	}
</script>

<style>
	page {
		background: #fafafa;
	}
</style>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100vh;
		overflow-y: hidden;
	}
</style>